package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 系统字典
 * 
 * @ClassName: DictionaryController
 * @Description: TODO
 * @author jumper
 * @date 2021-04-16 17:11:56
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/dictionary")
public class DictionaryController extends BaseController {

	private String PREFIX = "/backend/cdc/dictionary/";

	@Autowired
	private IDictionaryService dictionaryService;
	

    /**
     * 跳转到系统字典列表首页
     */
    @RequestMapping("")
    @Permission("dictionary:list")
    public String index() {
        return PREFIX + "dictionary.html";
    }
	
    /**
     * 跳转到新增系统字典
     */
    @RequestMapping("/to_add")
    @Permission("dictionary:add")
    public String toAdd() {
        return PREFIX + "dictionary_add.html";
    }
	
	/**
     * 跳转到修改系统字典
     */
    @RequestMapping("/to_update/{dictionaryId}")
    @Permission("dictionary:update")
    public String toUpdate(@PathVariable Long dictionaryId, Model model) {
        DictionaryEntity dictionary = this.dictionaryService.selectById(dictionaryId);
        model.addAttribute("dictionary",dictionary);
        return PREFIX + "dictionary_edit.html";
    }

    /**
     * 获取系统字典列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("dictionary:list")
    public Object list(@RequestParam Map<String, Object> params) {
        List<DictionaryEntity> result = null;
        Page<DictionaryEntity> page = new PageFactory<DictionaryEntity>().defaultPage();
        if((CommonUtil.isNotEmpty(params.get("NotQuery")))&&(params.get("NotQuery").toString().equals("true"))){
            result = new ArrayList<DictionaryEntity>();
        }else{
            result = this.dictionaryService.page(page,params);
        }
        page.setRecords((List<DictionaryEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    @RequestMapping(value = "/query/{productCode}/{typeCode}")
    @ResponseBody
    @Permission("dictionary:list")
    public Object queryByTypeCode(@PathVariable String productCode,@PathVariable String typeCode) {
        List<DictionaryEntity> result = this.dictionaryService.queryByTypeCode(productCode,typeCode);
        
        return result;
    }

    /**
     * 新增系统字典
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("dictionary:add")
    @BussinessLog(value = "新增系统字典")
    public Object add(DictionaryEntity dictionary) {
        if (CommonUtil.isOneEmpty(dictionary)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        dictionary.insert();
        return R.ok();
    }

    /**
     * 删除系统字典
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("dictionary:delete")
    @BussinessLog(value = "删除系统字典")
    public Object delete(@RequestParam Long dictionaryId) {
	
        this.dictionaryService.deleteById(dictionaryId);

        return R.ok();
    }

    /**
     * 修改系统字典
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("dictionary:update")
    @BussinessLog(value = "修改系统字典")
    public Object update(DictionaryEntity dictionary) {
        if (CommonUtil.isOneEmpty(dictionary)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        DictionaryEntity old = this.dictionaryService.selectById(dictionary.getId());

        old.setCode(dictionary.getCode());
        old.setDescription(dictionary.getDescription());
        old.setDictypeId(dictionary.getDictypeId());
        old.setIsActive(dictionary.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
